<!--
 * @Description: 
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-03-09 20:41:17
 * @LastEditors: 任飞
 * @LastEditTime: 2020-07-02 00:51:25
 -->
<template>
  <div class="app-container">
    <eHeader :roles="roles" :menus="menus" :query="query" />
    <!--表格渲染-->
    <el-table v-loading="loading" ref="table" :data="menus" :tree-props="{children: 'children', hasChildren: 'hasChildren'}" row-key="id" element-loading-text="拼命加载中" border fit highlight-current-row>
      <el-table-column prop="resourceName" label="名称" />
      <el-table-column prop="resourceIcon" label="图标" align="center" width="80px">
        <template slot-scope="scope">
          <svg-icon :icon-class="scope.row.resourceIcon" />
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="resourceNo" label="资源编号" />
      <el-table-column prop="resourceType" align="center" width="100px" label="资源类型">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.resourceType === "MENU" ? "菜单" : "按钮" }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="ifVisible" align="center" width="100px" label="是否显示">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.ifVisible === "Y" ? "显示" : "不显示" }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="sortOrder" align="center" width="100px" label="排序">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.sortOrder }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="routePath" label="链接地址" />
      <el-table-column :show-overflow-tooltip="true" prop="reqUrl" label="组件路径" />
      <el-table-column label="操作" width="200px" align="center">
        <template slot-scope="scope">
          <edit
            :roles="roles"
            :menus="menus"
            :data="scope.row"
            :sup_this="sup_this"
          />
          <el-popconfirm title="确定删除吗,如果存在下级节点则一并删除，此操作不能撤销！" @onConfirm="subDelete(scope.row.id)">
            <el-button slot="reference" type="danger" size="mini">删除</el-button>
          </el-popconfirm>
          <el-button v-if="scope.row.resourceLevel === 1" slot="reference" type="primary" size="mini" @click="test(scope.row.resourceNo)">新增</el-button>
          <eForm ref="form1" :roles="roles" :menus="menus" :is-add="true" :presource-no="scope.row.presourceNo"/>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission' // 权限判断函数
// import initData from '@/mixins/initData'
import treeTable from '@/components/TreeTable'
import { parseTime } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'
import eForm from './module/form'
export default {
  components: { eHeader, edit, treeTable, eForm },
  // mixins: [initData],
  data() {
    return {
      delLoading: false,
      sup_this: this,
      menus: [],
      roles: [],
      resourceNo: ""
    }
  },
  created() {
    // this.getRoles()
    this.getMenus()
    // this.$nextTick(() => {
    //   this.init()
    // })
  },
  methods: {
    test(code) {
      // debugger
      // console.log(code)
      // this.resourceNo = code
      // debugger
      this.$refs.form1.updateNo(code)
      this.$refs.form1.dialog = true
    },
    parseTime,
    checkPermission,
    beforeInit() {
      this.url = '/admin/sysResource/listTree'
      const query = this.query
      const value = query.value
      this.params = { pageNum: this.page, pageSize: this.limit }
      if (value) {
        this.params['name'] = value
      }
      return true
    },
    subDelete(id) {
      this.delLoading = true
      this.$api.fenxiao.menu
        .delMenu(id)
        .then(res => {
          this.delLoading = false
          this.getMenus()
          this.$notify({ title: '删除成功', type: 'success', duration: 2500 })
        })
        .catch(err => {
          this.delLoading = false
          console.log(err.response.data.message)
        })
    },
    getMenus() {
      this.$api.fenxiao.menu.listTree({}).then(res => {
        this.menus = []
        this.menus = res.data
      })
    },
    getRoles() {
      this.roles = []
      this.$api.permission.role.getRoleTree({}).then(res => {
        this.roles = res.data
      })
    }
  }
}
</script>

<style scoped>
</style>
